package cn.itcast.bos.web.action.base;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.service.base.AreaService;
import cn.itcast.bos.utils.PinYin4jUtils;
import cn.itcast.bos.web.action.common.BaseAction;

@Controller
@Namespace("/")
@ParentPackage("struts-default")
@Scope("prototype")
public class AreaAction extends BaseAction<Area> {

	private File file;
	private String fileFileName;

	public void setFile(File file) {
		this.file = file;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	@Autowired
	private AreaService areaService;

	@Action(value = "area_upload")
	public void addAll() throws FileNotFoundException, IOException {
		// 定义批量数据的容器
		List<Area> list = new ArrayList<>();
		// POI插件 HSSF解析xls XSSF解析xlsx
		Workbook workBook;
		// 判断文件类型并解析文件
		if (fileFileName.endsWith(".xls")) {
			workBook = new HSSFWorkbook(new FileInputStream(file));
		} else {
			workBook = new XSSFWorkbook(new FileInputStream(file));
		}
		// 获取文件第一sheet
		Sheet sheet = workBook.getSheetAt(0);
		// 遍历sheet获取每一行数据,再通过每一行get方法获取每一列的数据
		for (Row row : sheet) {
			Area areaOne = new Area();
			// 忽略第一行
			if (row.getRowNum() == 0) {
				continue;
			}
			// 忽略第一个列为空的数据
			if (row.getCell(0) == null || StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
				continue;
			}
			// 获取每一列并封装到实例对象中
			areaOne.setId(row.getCell(0).getStringCellValue());
			areaOne.setProvince(row.getCell(1).getStringCellValue());
			areaOne.setCity(row.getCell(2).getStringCellValue());
			areaOne.setDistrict(row.getCell(3).getStringCellValue());
			areaOne.setPostcode(row.getCell(4).getStringCellValue());
			// 基于pinyin4j生产简码和城市编码
			String province = areaOne.getProvince();
			String city = areaOne.getCity();
			String district = areaOne.getDistrict();
			province = province.substring(0, province.length() - 1);
			city = city.substring(0, city.length() - 1);
			district = district.substring(0, district.length() - 1);
			String[] headArray = PinYin4jUtils.getHeadByString(province + city + district);
			StringBuffer sb = new StringBuffer();
			for (String string : headArray) {
				sb.append(string);
			}
			String shortcode = sb.toString();
			String citycode = PinYin4jUtils.hanziToPinyin(city, "");
			areaOne.setCitycode(citycode);
			areaOne.setShortcode(shortcode);
			// 添加到容器里
			list.add(areaOne);
		}
		areaService.add(list);
		workBook.close();
	}

	@Action(value = "area_showPage")
	public void showPage() {
		Pageable pageable = new PageRequest(page, rows);
		Specification<Area> specification = new Specification<Area>() {

			@Override
			public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<>();
				if (StringUtils.isNotBlank(model.getProvince())) {
					Predicate p1 = cb.like(root.get("province").as(String.class), "%" + model.getProvince() + "%");
					list.add(p1);
				}
				if (StringUtils.isNotBlank(model.getCity())) {
					Predicate p2 = cb.like(root.get("city").as(String.class), "%" + model.getCity() + "%");
					list.add(p2);
				}
				if (StringUtils.isNotBlank(model.getDistrict())) {
					Predicate p3 = cb.like(root.get("district").as(String.class), "%" + model.getDistrict() + "%");
					list.add(p3);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		Page<Area> page = areaService.findAll(pageable, specification);
		writeJson(page);
	}
}
